package com.example.ssm.controller;

import com.example.ssm.common.Result;
import com.example.ssm.pojo.DrillPlan;
import com.example.ssm.pojo.dto.DrillPlanDTO;
import com.example.ssm.pojo.dto.DrillPlanPageDTO;
import com.example.ssm.pojo.resp.DrillPlanPageResp;
import com.example.ssm.service.DrillPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drillPlan")
public class DrillPlanController {
    @Autowired
    private DrillPlanService drillPlanService;

    /**
     * 新增、修改演练方案
     */
    @PostMapping("/save")
    public Result save(Integer userId, DrillPlanDTO drillPlanDTO) {
        DrillPlan drillPlan = drillPlanService.saveDrillPlan(userId, drillPlanDTO);
        if (drillPlan != null) {
            return Result.success(drillPlan.getId());
        } else {
            return Result.error("保存演练方案失败");
        }
    }

    /**
     * 获取演练方案详情
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        DrillPlan drillPlan = drillPlanService.getById(id);
        if (drillPlan != null) {
            return Result.success(drillPlan);
        } else {
            return Result.error("获取演练方案详情失败");
        }
    }

    /**
     * 审核演练方案
     */
    @PostMapping("/audit")
    public Result audit(Integer userId, Long drillPlanId, String status) {
        Boolean isSuccess = drillPlanService.audit(userId, drillPlanId, status);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("审核演练方案失败");
        }
    }

    /**
     * 删除演练方案
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long drillPlanId) {
        Boolean isSuccess = drillPlanService.removeById(drillPlanId);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("删除演练方案失败");
        }
    }

    /**
     * 分页条件查询演练方案
     */
    @PostMapping("/list")
    public Result<List<DrillPlanPageResp>> list(@RequestBody DrillPlanPageDTO drillPlanPageDTO) {
        List<DrillPlanPageResp> drillPlanPageRespList = drillPlanService.pageQuery(drillPlanPageDTO);
        return Result.success(drillPlanPageRespList);
    }
}
